package validation;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import dbmanager.DBManager;

public class Isinbetweendate implements Validation 
{
	String ConditionValue;
	String toDay;
	DBManager database;

	public String getMessage()
	{	
		return " Date must be between "+ConditionValue + " and " +toDay ;
	}

	public void setDbmanager(DBManager database) 
	{
		this.database =database;
	}

	public boolean validate(String Value) 
	{
		return false;
	}

	public boolean validate(String Value, String ConditionValue) 
	{
		if(Value.length()==0 && ConditionValue.length()==0) return true;
		try
		{
			String Datechar=Character.toString(Value.charAt(getSpChar(0, Value.toCharArray())));  
			Date valuedate = null ;
			try 
			{
				String pattern = "yyyy"+Datechar+"MM"+Datechar+"dd hh:mm:ss";
				SimpleDateFormat sdm = new SimpleDateFormat(pattern);
				valuedate = (Date)sdm.parse(Value);
			} catch (Exception e) 
			{
				try 
				{
					String pattern = "yyyy"+Datechar+"MM"+Datechar+"dd";
					SimpleDateFormat sdm = new SimpleDateFormat(pattern);
					valuedate = (Date)sdm.parse(Value);
				} catch (Exception e1) {
				}
			}
			
			Date conditionvaluedate = null;
			Date today = null;
			try 
			{
				String pattern = "yyyy"+Datechar+"MM"+Datechar+"dd hh:mm:ss";
				SimpleDateFormat sdm = new SimpleDateFormat(pattern);
				conditionvaluedate = (Date)sdm.parse(ConditionValue);
				today = new Date();
				this.ConditionValue = ConditionValue.toString();
				toDay = today.toString();
			} catch (Exception e) 
			{
				try 
				{
					String pattern = "yyyy"+Datechar+"MM"+Datechar+"dd";
					SimpleDateFormat sdm = new SimpleDateFormat(pattern);
					conditionvaluedate = (Date)sdm.parse(ConditionValue);
					today = new Date();
					this.ConditionValue = ConditionValue.toString();
					toDay = today.toString();
				} catch (RuntimeException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}
			
			int i=today.compareTo(conditionvaluedate);
			int j=today.compareTo(valuedate);
			if(today.compareTo(conditionvaluedate)==-1 && today.compareTo(valuedate)==1)
			{
				System.out.println("Todays date is :"+today.toString());
				System.out.println("FD :"+Value.toString());
				System.out.println("TD :"+ConditionValue.toString());
				return true;
			}
			System.out.println(today.toString());
			System.out.println(i);
			System.out.println(j);
		} 
		catch (ParseException e) 
		{
			e.printStackTrace();
		}
			return false;
	}

	public boolean validate(String Value, String ConditionValue, String Refvalue) 
	{
		this.ConditionValue = ConditionValue;
		return validate(Value,ConditionValue);
	}
	
	public static void main(String[] args) 
	{
		Isinbetweendate is = new  Isinbetweendate();
		System.out.println(is.validate("2007-05-01", "2007-08-11"));
		System.out.println(is.validate("2007-05-30 00:00:00","2007-05-30 15:10:01"));
		System.out.println(is.getMessage());
	}
	
	public int getSpChar(int y, char[] msg)
	{
		char stringChar='"';
		//int [] specialChar = {33,35,36,37,38,40,41,42,43,44,58,59,60,62,63,64,91,92,93,94,96,123,124,125,126};
		int [] specialChar = {32,33,34,35,36,37,38,40,41,42,43,44,45,46,47,58,59,60,61,62,63,64,91,92,93,94,95,96,123,124,125,126};
		for(int i=y+1;i<msg.length;i++)
		{
			//System.out.println("Char is : "+msg[i]+"\n String char is : "+stringChar);
			if(stringChar == msg[i])
			{
				i++;
				while(msg[i]!=stringChar)
					i++;
				return i+1;
			}
			//System.out.println(msg[i]);
			for(int j=0;j<specialChar.length;j++)
			{
				if(msg[i]==(char)specialChar[j])
				{
					return i;
				}
			}
		}
	return msg.length;
	}

	public void setBasicvalue(String value) {
		// TODO Auto-generated method stub
		
	}
}

